package com.example.mvvm.base.data;

import androidx.annotation.Nullable;
import androidx.lifecycle.MutableLiveData;

import java.net.PortUnreachableException;
import java.util.ArrayList;
import java.util.List;

public class MutableLiveListData<T> extends MutableLiveData<List<T>> {

    public static class Builder<T> {
        public MutableLiveListData<T> mutableLiveListData;

        public Builder(MutableLiveListData<T> mutableLiveListData) {
            this.mutableLiveListData = mutableLiveListData;
        }

        public List<T> entity;

        public Builder add(T data) {
            if (entity == null) {
                entity = new ArrayList<T>();
            }
            entity.add(data);
            return this;
        }

        public void buildPost() {
            mutableLiveListData.postValue(entity);
        }

        public void buildSet() {
            mutableLiveListData.setValue(entity);
        }
        public void buildAdd() {
            mutableLiveListData.addData(entity);
        }
    }

    public void addData(T data) {
        List<T> value = this.getValue();
        if (value == null) {
            value = new ArrayList<T>();
        }
        value.add(data);
        this.setValue(value);
    }

    public void addData(List<T> list) {
        if (list != null && list.size() > 0) {
            List<T> value = getValue();
            value.addAll(list);
            setValue(value);
        }
    }

    public void addData(T... data) {
        List<T> value = this.getValue();
        if (value == null) {
            value = new ArrayList<T>();
        }
        for (T datum : data) {
            value.add(datum);
        }
        this.setValue(value);
    }

    public void setData(int index, T data) {
        List<T> value = this.getValue();
        if (value != null) {
            value.set(index, data);
            this.setValue(value);
        }
    }

    public T getData(T data) {
        List<T> value = this.getValue();
        if (value != null) {
            if(value.contains(data)){
                return value.get(value.indexOf(data));
            }
        }
        return null;
    }

    public void indexOf(T data) {
        List<T> value = this.getValue();
        if (value != null) {
            if (value.contains(data)) {
                value.set(value.indexOf(data), data);
            } else {
                addData(data);
            }
        } else {
            addData(data);
        }
        this.setValue(value);
    }

    public void deleteList(T data) {
        if (data != null) {
            List<T> value = this.getValue();
            if (value != null) {
                if(value.contains(data)){
                    value.remove(data);
                    this.setValue(value);
                }
            }
        }
    }

    public void clean() {
        List<T> value = this.getValue();
        if (value != null) {
            value.clear();
            this.setValue(value);
        }
    }


    @Nullable
    @Override
    public List<T> getValue() {
        List<T> value = super.getValue();
        if (value == null) {
            value = new ArrayList<>();
        }
        return value;
    }

    public int getSize(){
        List<T> value = super.getValue();
        if (value == null) {
            return 0;
        }
        return value.size();
    }

    public void callValue(){
        postValue(getValue());
    }

    public void synchronouscallValue(){
        setValue(getValue());
    }


}
